﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RuoVea.GoFastdfs.Common
{
    /// <summary>
    /// 文件大小转换工具类
    /// </summary>
    public class FileSizeUtil
    {
        /// <summary>
        /// 根据文件大小转换为B、KB、MB、GB单位字符串显示
        /// </summary>
        /// <param name="filesize">文件的大小（long型）</param>
        /// <returns>转换后带有单位的字符串</returns>
        public static String GetLength(long filesize)
        {
            String strFileSize = null;
            if (filesize < 1024)
            {
                strFileSize = filesize + "B";
                return strFileSize;
            }
    
            if ((filesize >= 1024) && (filesize < 1024 * 1024))
            {
                strFileSize = Math.Round((((double)filesize) / 1024),2) + "KB";
            }
            else if ((filesize >= 1024 * 1024) && (filesize < 1024 * 1024 * 1024))
            {
                strFileSize = Math.Round((((double)filesize) / (1024 * 1024)),2) + "MB";
            }
            else
            {
                strFileSize = Math.Round((((double)filesize) / (1024 * 1024 * 1024)),2) + "GB";
            }

            return strFileSize;
        }

        /// <summary>
        /// 文件的大小（long型）
        /// </summary>
        /// <param name="filesize"></param>
        /// <returns>转换后不带单位的字符串</returns>
        public static String GetMBLength(long filesize)
        {
            return  (((double)filesize) / (1024 * 1024))+"";
        }
    }
}
